package com.cuitrip.business.user.ui;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.business.location.model.AreaMode;
import com.cuitrip.business.location.model.LocationMode;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.business.user.Indexer;
import com.cuitrip.service.R;
import com.lab.component.list.adapter.CTListAdapter;

import java.util.HashMap;

/**
 * Created by sheena on 16/3/4.
 */
public class CityChooseAdapter extends CTListAdapter implements Indexer{
    LocationMode locationMode;
    HashMap<String,Integer> indexMap;


    public void setData(LocationMode locationMode,HashMap<String,Integer> indexMap) {
        this.locationMode = locationMode;
        this.indexMap=indexMap;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return locationMode == null ? 0 : locationMode.getContent().size();
    }

    @Override
    public AreaMode getItem(int position) {
        return locationMode.getContent().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        String mFirstChar=null;
        String mPreFirstChar=null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city_choose, parent, false);
            holder = new ViewHolder();
            convertView.setTag(holder);
            holder.index= (TextView) convertView.findViewById(R.id.index);
            holder.country = (TextView) convertView.findViewById(R.id.item_name);
            holder.icon = (IconTextView) convertView.findViewById(R.id.icon);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        AreaMode areaMode = getItem(position);
        if(position == 0) {
            mPreFirstChar = "-";
        }else {
            String preFirstChar =getItem(position-1).getFirstChar();
            if (!TextUtils.isEmpty(preFirstChar)){
                mPreFirstChar = String.valueOf(preFirstChar.charAt(0)) ;
            }
        }
        String firstChar =areaMode.getFirstChar();
        if (!TextUtils.isEmpty(firstChar)){
            mFirstChar =String.valueOf(firstChar.charAt(0));
        }
        if(!TextUtils.equals(mPreFirstChar, mFirstChar)){
            holder.index.setVisibility(View.VISIBLE);
            holder.index.setText("      "+mFirstChar);
        }else{
            holder.index.setVisibility(View.GONE);
        }
        holder.country.setText(areaMode.getName());
        holder.icon.setVisibility(areaMode. getHasChild()==0? View.INVISIBLE : View.VISIBLE);
        return convertView;
    }

    @Override
    public int getStartPositionOfSection(String section) {
        if(indexMap.containsKey(section))
            return indexMap.get(section);
        return -1;
    }


    static class ViewHolder {
        TextView country;
        IconTextView icon;
        TextView index;
    }
}
